package com.wy.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class MQListener {
    @RabbitListener(queues = "www")
    public void listen01(String msg) {
        System.out.println("消费者收到了 www 的消息:[" + msg + "]");
    }

    @RabbitListener(queues = "work")
    public void listen02(String msg) throws InterruptedException {
        System.out.println("消费者1收到了 work 的消息:[" + msg + "]");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work")
    public void listen03(String msg) throws InterruptedException {
        System.err.println("消费者2收到了 work 的消息:[" + msg + "]");
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout1")
    public void listen04(String msg1) {
        System.out.println("消费者1收到了 fanout1 的消息:[" + msg1 + "]");
    }

    @RabbitListener(queues = "fanout2")
    public void listen05(String msg) {
        System.err.println("消费者2收到了 fanout2 的消息:[" + msg + "]");
    }

    @RabbitListener(queues = "direct1")
    public void listen06(String msg) {
        System.out.println("消费者1收到了 direct1 的消息:[" + msg + "]");
    }

    @RabbitListener(queues = "direct2")
    public void listen07(String msg) {
        System.err.println("消费者2收到了 direct2 的消息:[" + msg + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "nnn", durable = "true"),
            exchange = @Exchange(name = "mm", type = ExchangeTypes.DIRECT),
            key = {"aa", "bb"}
    ))
    public void listen08(String msg) {
        System.err.println("消费者2收到了 direct2 的消息:[" + msg + "]");
    }

    @RabbitListener(queues = "dlx.queue")
    public void listen09(String msg){
        log.info("消费者收到了 dlx.queue 的消息:[" + msg + "]");
    }
    //加延迟插件
    //在exchange中的属性delayed=true
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "delay.queue",durable = "true"),
            exchange = @Exchange(value = "delay.direct"),
            key = "hi"
    ))
    public void listen10(String msg){
        log.info("接受到delay.queue的消息：{}"+msg);
    }
}
